//
//  RequestUtils.m
//  Pods
//
//  Created by zhangwenchao on 16/11/24.
//
//

#import "RequestUtils.h"
#import <CommonCrypto/CommonDigest.h>

@implementation RequestUtils


/**
 *  保存用户数据到plist
 *
 *  @param dic JSON
 */
+ (void)saveUserData :(NSDictionary*)dic {
    // 转为data
    NSError *error;
    
    if (!dic) {
        return;
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    // 创建
    NSMutableDictionary *userData = [[NSMutableDictionary alloc]init];
    [userData setObject:data forKey:@"user"];
    // 沙盘目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    //取得完整的文件名
    NSString *fileName = [plist stringByAppendingPathComponent:@"user.plist"];
    //创建并写入文件
    [userData writeToFile:fileName atomically:YES];
}

/**
 *  从plist获取用户数据
 */
+ (NSDictionary *)getUserData {
    // 获取plist数据
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:@"user.plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc]initWithContentsOfFile:fileName];
    // 恢复为字典数据
    if ([data objectForKey:@"user"] != nil ) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[data objectForKey:@"user"] options:NSJSONReadingMutableLeaves error:nil];
        return dic;
    }
    return nil;
}

/**
 *  保存token到plis
 *
 *  @param token
 */
+ (void)saveToken:(NSString *)token {
    // 保存到plist
    NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
    if (token) {
        [data setObject:token forKey:@"token"];
        
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *plist = [path objectAtIndex:0];
        NSString *fileName = [plist stringByAppendingPathComponent:@"token.plist"];
        [data writeToFile:fileName atomically:YES];
    }
    
    
}

/**
 *  从plist获取token
 *
 *  @return token值
 */
+ (NSString *)getToken {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:[NSString stringWithFormat:@"token.plist"]];
    NSDictionary *data = [[NSMutableDictionary alloc]initWithContentsOfFile:fileName];
    NSString *token = [data objectForKey:@"token"];
    return token;
}

/**
 *  MD5加密
 *
 *  @param string 需加密字符串
 *
 *  @return 加密字符串
 */
+ (NSString *)md5:(NSString *)string {
    const char *cStr = [string UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result);
    return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

@end
